package com.kestrel.space.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.kestrel.space.common.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.user.mapper.UserMapper;
import com.kestrel.space.user.model.User;
import com.kestrel.space.user.query.UserQuery;
import com.kestrel.space.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kestrel.space.common.utils.BizException;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.kestrel.space.common.utils.JwtUtil.JWT_TTL;

/**
 * @author yebolin
 * @date 2024/8/21
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> listUsers(UserQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        User user = BeanUtil.copyProperties(query, User.class);
        return userMapper.select(user);
    }

    @Override
    public String saveOrUpdateUser(User user, String type) {
        if(type.equals("save")){
            user.setId(Generator.generateId());
            Long account = userMapper.selectMaxAccount();
            if(account == null){
                account = 10000001L;
            }
            user.setAccount(account + 1);
            user.setIfDeleted(false);
            userMapper.insert(user);
        } else if(type.equals("update")){
            userMapper.updateByPrimaryKey(user);
        } else {
            throw new BizException("type 类型出错，请输入 save 或者 update");
        }

        return "编号：" + user.getId() + " 用户新增/更新成功";
    }

    @Override
    public String batchSaveOrUpdateUsers(List<User> userList, String type) {

        if(type.equals("save")){
            userList.forEach(user -> {
                user.setId(Generator.generateId());
                userMapper.insert(user);
            });
        } else if(type.equals("update")){
            userList.forEach(user -> userMapper.updateByPrimaryKey(user));
        } else {
            throw new BizException("type 类型出错，请输入 save 或者 update");
        }

        return "批量新增/更新用户成功";
    }

    @Override
    public List<String> login(User user) {
        List<String> message = new ArrayList<>();
        message.add("登录成功");
        User userR = new User();
        userR.setAccount(user.getAccount());
        userR = userMapper.selectOne(userR);
        if(!userR.getPassword().equals(user.getPassword())){
            message.remove(0);
            message.add("登录失败");
            message.add("");
        } else {
            String jwt = JwtUtil.createJWT(String.valueOf(userR.getId()), JSONObject.toJSONString(userR), JWT_TTL);
            message.add(jwt);
        }
        return message;
    }
}
